#-*- coding: UTF-8 -*-
import urllib
from urllib import parse
import urllib.request
import base64
import json
from objprint import objprint
import hashlib, time
import hmac
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.iai.v20200303 import iai_client, models

SecretId = ''
SecretKey = ""
url_preffix = 'https://iai.tencentcloudapi.com'
Version = '2020-03-03'
class AiPlat(object):
    def __init__(self, app_id, app_key):
        self.app_id = app_id
        self.app_key = app_key
        self.data = {}
        self.url_data = ''

    def invoke(self, _params):
        try:
           cred = credential.Credential(SecretId, SecretKey)
    # 实例化一个http选项，可选的，没有特殊需求可以跳过
           httpProfile = HttpProfile()
           httpProfile.endpoint = "iai.tencentcloudapi.com"

    # 实例化一个client选项，可选的，没有特殊需求可以跳过
           clientProfile = ClientProfile()
           clientProfile.httpProfile = httpProfile
    # 实例化要请求产品的client对象,clientProfile是可选的
           client = iai_client.IaiClient(cred, "ap-chengdu", clientProfile)

    # 实例化一个请求对象,每个接口都会对应一个request对象
           req = models.DetectFaceRequest()
           params = {
               "Image":_params,
               "NeedFaceAttributes":1
           }
           req.from_json_string(json.dumps(params))

    # 返回的resp是一个DetectFaceResponse的实例，与请求对象对应
           resp = client.DetectFace(req)
    # 输出json格式的字符串回包
           res_dict = json.loads(resp.to_json_string())
           res_dict['ret'] = 0
           return(res_dict)

        except TencentCloudSDKException as err:
           print(err)
           return {'ret': -1}

    def face_detectface(self, image, mode):
        self.url = url_preffix
        image_data = str(base64.b64encode(image),'utf-8')
        return self.invoke(image_data)

